package com.wanxi.servlet.customer;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.CustomerModel;
import com.wanxi.service.CustomerService;
import com.wanxi.service.Impl.CustomerServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/5 20:16
 */
@WebServlet("/back/UpdateCustomer")
public class UpdateCustomerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2.获取前端数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String email = req.getParameter("email");
        String enable = req.getParameter("enable");
        String remark = req.getParameter("remark");
        String lawyerId = req.getParameter("lawyer_id");
        //3.封装model
        CustomerModel customerModel = CustomerModel.builder()
                .id(Tool.nullToInt(id))
                .name(Tool.nullToStr(name))
                .age(Tool.nullToInt(age))
                .sex(Tool.nullToStr(sex))
                .email(Tool.nullToStr(email))
                .phone(Tool.nullToStr(phone))
                .address(Tool.nullToStr(address))
                .enable(Tool.nullToInt(enable))
                .remark(Tool.nullToStr(remark))
                .lawyerId(Tool.nullToInt(lawyerId))
                .build();
        //4.调用service层
        CustomerService customerService = new CustomerServiceImpl();
        ResultDto resultDto = customerService.update(customerModel);
        //5.返回前台数据
        resp.getWriter().println(JSONObject.toJSONString(resultDto));

    }
}
